<?php

/**
 * 用于处理第三方接口回调（支付，公众号token等）
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\index\controller;

use app\common\controller\Front;

class Api extends Front
{

    //第三方回调
    function payment()
    {
        $code = input('code');
        if ($code === "kqpay") {
            $data = [
                'payment_code' => 'kqpay',
                'out_trade_no' => input('orderId'),
                'extra_common_param' => input('ext1'),
                'trade_no' => input('dealId')
            ];
        } else {
            return '';
        }

        $order_type = $data['extra_common_param'];
        if ($order_type == 'real_order') {
            $act = 'member/order/index';
        } elseif ($order_type == 'vr_order') {
            $act = 'member/order/vr';
        } elseif ($order_type == 'pd_order') {
            $act = 'memberorder/predeposit';
        } else {
            exit();
        }

        $out_trade_no = $data['out_trade_no'];
        $trade_no = $data['trade_no'];
        $url = url($act);

        //对外部交易编号进行非空判断
        if (!preg_match('/^\d{16,18}$/', $out_trade_no)) {
            return $this->error('参数错误', $url);
        }

        $logic_payment = model('Common/Payment', "logic");

        if ($order_type == 'real_order') {
            $result = $logic_payment->getRealOrderInfo($out_trade_no);
            if (!$result['status']) {
                return $this->error($result['msg'], $url);
            }
            if ($result['data']['api_pay_state']) {
                $payment_state = 'success';
            }
            $order_list = $result['data']['order_list'];
        } else {
            exit('error');
        }

        $order_pay_info = $result['data'];
        $api_pay_amount = $result['data']['api_pay_amount'];

        if ($payment_state != 'success') {
            //取得支付方式
            $result = $logic_payment->getPaymentInfo($data['payment_code']);
            if (!$result['status']) {
                return $this->error($result['msg'], $url);
            }
            $payment_info = $result['data'];

            //创建支付接口对象
            $cls = '\\payment\\' . $payment_info['payment_code'];
            $payment_api = new $cls($payment_info, $order_pay_info);

            //返回参数判断
            $verify = $payment_api->return_verify();
            if (!$verify) {
                return $this->error('支付数据验证失败', $url);
            }

            //取得支付结果
            $pay_result = true; //$payment_api->getPayResult(input('get.'));
            if (!$pay_result) {
                return $this->error('非常抱歉，您的订单支付没有成功，请您后尝试', $url);
            }

            //更改订单支付状态
            if ($order_type == 'real_order') {
                $result = $logic_payment->updateRealOrder($out_trade_no, $payment_info['payment_code'], $order_list, $trade_no);
            }

            if (!$result['status']) {
                return $this->error('支付状态更新失败', $url);
            }
            
            //快钱支付成功,输出内容给快钱,它返回后再跳转
            if ($data['payment_code'] == 'kqpay' && $verify) {
                echo '<result>1</result>' . ' <redirecturl>' . $payment_api->getRtnUrl() . '</redirecturl>';
                exit;
            }
        }

        //支付成功后跳转
        if ($order_type == 'real_order') {
            $pay_ok_url = '/?s=member/buy/pay_ok&pay_sn=' . $out_trade_no . '&pay_amount=' . ncPriceFormat($api_pay_amount);
        }
        
        return redirect($pay_ok_url);
    }

}
